<?php
namespace app\home\controller;
use think\Db;
use think\Request;
class Calculator extends Common{
    public function _initialize()
    {
        parent::_initialize();
    }


    public function index(){

        if(request()->isPost())
        {
            $param=$_POST;
            //查询利率
            $param['loan_id']=$param['loan'];
            $param['Monthly_interest_rate']=db('calculator_limit')->where(array('id'=>$_POST['limit']))->value('rate');
            $param['loan']=db('calculator_limit')->where(array('id'=>$_POST['limit']))->value('money');
            $data=$this->getDate($param);
            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$data,'rel'=>1];
        }else{
            $mode=!input('param.mode')?1:input('param.mode');
            $this->assign('mode',$mode);

            $loan=db('loan')->select();
            $this->assign('loan',$loan);


            $insurance_three=db('calculator_insurance')->where(array('type'=>$mode))->select();
            $this->assign('insurance_three',$insurance_three);

            $limit=db('calculator_limit')->where(array('type'=>$mode))->select();
            $this->assign('limit',$limit);


            $stage=db('calculator_stage')->where(array('type'=>$mode))->select();
            $this->assign('stage',$stage);


            return $this->fetch();
        }        

    }


    /**
     * 
     * 数据接口
     * mode,$car_price,$loan,$insurance_three,$Stage
     * @return [type] [description]
     */
    private function getDate($param)
    {
        // print_r($param);die;
       /* $species='挖掘机';
        $car_price=530000;//车价
        $loan=265000;
        $insurance_three=500000;
        $Stage=12;*/

        if($param['mode']==1){
                
                $car_price=$param['car_price'];//车价
                $loan=(1-$param['loan'])*$car_price;
                $insurance_three=$param['insurance_three'];
                $Stage=$param['stage'];


                $Monthly_interest_rate=$param['Monthly_interest_rate'];//每月利息利率
                $Monthly_interest=$Monthly_interest_rate*$loan;//每月利息
                $Monthly_payments=$Monthly_interest+$loan/$Stage;//每月还款

                $Total_interest=$Monthly_interest*$Stage;//总利息


                $GPS=1500;
                if($Stage>12){
                 $GPS=2000;
                }

                $service_fee_rate=0.05;//服务费利率
                $margin_rate=0.04;//保证金利率

                $service_fee=$service_fee_rate*$loan;//服务费
                $margin=$margin_rate*$loan;//保证金

                $insurance=$this->get_insurance($car_price,$insurance_three,$Stage,$param['loan_id']);//保险

                $down_payment=$GPS+$service_fee+$margin+$insurance+$car_price*$param['loan'];
                $total_amount=$car_price+$Total_interest+$GPS+$insurance+$service_fee;
                /*echo $down_payment;
                echo "-";
                echo $total_amount;*/ 

                $data = array(
                    'mode'=>1,
                    'Total_interest' => $Total_interest, 
                    'Monthly_interest' => $Monthly_interest, 
                    'total_amount' => $total_amount, 
                    'down_payment' => $down_payment, 
                    'loan' => $loan,  //贷款总额
                    'insurance' => $insurance, 
                    'margin' => $margin, 
                    'service_fee' => $service_fee, 
                    'GPS' => $GPS, 
                    // 'other_fee' => $other_fee, 
                    'Monthly_payments' => $Monthly_payments
                );  

        }else if($param['mode']==2){
            //抵押
            $car_price=$param['car_price'];//车价
            $loan=(1-$param['loan'])*$car_price;
            $insurance_three=$param['insurance_three'];
            $Stage=$param['stage'];            

            $Monthly_interest_rate=$param['Monthly_interest_rate'];//每月利息利率
            $Monthly_interest=$Monthly_interest_rate*$loan;//每月利息
            $Monthly_payments=$Monthly_interest+$loan/$Stage;//每月还款

            $Total_interest=$Monthly_interest*$Stage;//总利息

            $GPS=1500;
            if($Stage>12){
             $GPS=2000;
            }

            $service_fee_rate=0.03;//服务费利率
            $service_fee=$service_fee_rate*$loan;//服务费

            $insurance=$this->get_insurance($car_price,$insurance_three,$Stage,$param['loan_id']);//保险

            $poundage=$GPS+$service_fee+$insurance;
            $total_cost=$Total_interest+$poundage;

            $data = array(
                'mode'=>2,
                'Total_interest' => $Total_interest, 
                'Monthly_interest' => $Monthly_interest, 
                'insurance' => $insurance, 
                'loan' => $loan,  //贷款总额
                'service_fee' => $service_fee, 
                'GPS' => $GPS, 
                'Monthly_payments' => $Monthly_payments,
                'poundage' => $poundage,
                'total_cost' => $total_cost,
            );  
        }
        return $data;
    }
 

    /**
     * 获取保险金
     * @param  [type] $Stage [分期月]
     * @param  [type] $Stage [ID]
     * @return [type]        [description]
     */
     private function get_insurance($car_price,$insurance_three,$Stage,$loan_id)
    {
        $result=0;
        $arr=db('insurance')->where(array('type'=>$loan_id,'open'=>1))->select();
        //第三者费率
        $insurance_three_rate=db('loan')->where(array('id'=>$loan_id))->value('rate');
            // echo $insurance_three_rate;
            // print_r($arr);die;
            foreach ($arr as $key => $value) {
                // echo $car_price*$value['rate']."-";
                   $result=$result+$car_price*$value['rate'];
            }    
            // echo $insurance_three*$insurance_three_rate*0.01;die;
            $result=$result+$insurance_three*$insurance_three_rate*0.01;  

        if($Stage>12){
            return $result*2;
        }else{
            return $result;
        }
    }
   
}

